/**
 * Created with IntelliJ IDEA
 * Description:类 初学习
 * User:22954
 * Date:2024-01-06
 * Time:16:14
 */
class Students {
    public String name;
    public int age;
    public String sex;
    public static String classes;//static修饰的类变量，只能通过类去访问

    {
        //在调用构造方法前被执行
        //一般用来初始化成员变量/非静态变量
        System.out.println("实例代码块");
    }

    static {
        //在类被加载时就执行，执行优先级高，切一个程序仅执行一次
        //一般用来初始化静态变量
        Students.classes = "2";
        System.out.println("静态代码块");
    }

    static {
        //多个静态代码块按顺序执行
        System.out.println("22222");
    }

    public Students(int i) {

    }

    public Students() {
        this("wangwu", 20);//this()为调用其他构造方法，this()必须在构造方法的第一行
        System.out.println("haha");
        Students.classes = "20";
        /*this("wangwu",20);*/
    }

    public Students(String name, int age) {
        /*this();*/ //this()不能形成循环
        this.name = name;
        this.age = age;
        System.out.println("hehe");
    }

    public void setData(String name, int age, String sex) {
        this.name = name;//this.调用成员变量
        this.age = age;
        this.sex = sex;
    }

    public void printData() {
        System.out.println("名字:" + this.name + " 性别:" + this.sex + " 年龄:" + this.age + " 班级:" + Students.classes);//非静态方法可以调用静态变量
    }

    public void fun() {
        this.printData();//this.调用成员方法
    }

    public static void fuc()/*静态方法，不由对象引用，由类引用*/ {
        /*this.name="lisi";*/ //this.访问对象
        /*fun();*/  //静态方法不能调用非静态方法
        Students students=new Students();  //
        students.age=12;                   //静态方法访问类变量，需新建一个对象，由对象访问
        System.out.println(students.age);  //
        System.out.println("aaa");
    }
/*    public int fuc1(){
        static int i=0;
        return i;
    }*/  //在类方法中不能定义静态变量，方法中变量属性为局部变量，静态变量属性为类变量，冲突，编译错误

}

public class Test {
    public static void main(String[] args) {
        Students.fuc();
    }
    public static void main4(String[] args) {
        Students students = new Students();
        students.classes = "100";
        System.out.println(Students.classes);
    }

    public static void main3(String[] args) {
        System.out.println(Students.classes);
    }

    public static void main2(String[] args) {
        Students students = new Students(1);
        Students.classes = "19";
        Students students2 = new Students(2);
    }

    public static void main1(String[] args) {
        Students students1 = new Students();
        students1.printData();
        Students.classes = "2023215120";//通过类名去访问静态变量，而不是通过变量名访问
        students1.printData();
        Students.fuc();
    }
}
